<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ taglib uri="/WEB-INF/vectrics.tld"    prefix="vectrics" %>


<vectrics:errors/>
<%@ page import="org.vectrics.hibernate.*" %>

<%
	String personId = request.getParameter("personId");
	String selectParam = request.getParameter("selectParam");
	
	String userName = ThreadProperties.getPerson().getUser().getUserName();
	String password = ThreadProperties.getPerson().getUser().getPassword();
	String domainId = ThreadProperties.getDomain().getId().toString();
	String contextPath = request.getContextPath();
%>



<script type="text/javascript" language="javascript">
<!--
	var currentPersonNodes;

    var padre = window.opener;

	 function findMatches() { 
	 	log('findMatches() called');
	    var lastNameEdit = document.getElementById("lastName");
		var lastName = lastNameEdit.value;

	    var firstNameEdit = document.getElementById("firstName");
		var firstName = firstNameEdit.value;

	    var phoneEdit = document.getElementById("phoneNumber");
		var phoneNumber = phoneEdit.value;

	    var cityEdit = document.getElementById("city");
		var city = cityEdit.value;

	    var stateEdit = document.getElementById("state");
		var state = stateEdit.value;

	 	log('findMatches() - last name: ' + lastName);
		var href = '<%=contextPath%>/ws?command=get.matching.candidate&user=<%=userName%>&domainId=<%=domainId%>&pcode=<%=password%>&lastName=';
		href += lastName + '&firstName=' + firstName 
			+ '&phoneNumber=' + phoneNumber
			+ '&city=' + city
			+ '&state=' + state;
		remoteCall(href, handleXmlSearchResponse);
	 } 

	 function cancel() { 
	 	log('cancel() called');
		window.close();	
	 } 

	function findMatchesRemoteCall(lastName) {
		
	}

	 function clearPersonSearch() {
			var lastNameEdit = document.getElementById("lastName");
			lastNameEdit.value = '';

			var firstNameEdit = document.getElementById("firstName");
			firstNameEdit.value = '';

			var phoneEdit = document.getElementById("phoneNumber");
			phoneEdit.value = '';

			var cityEdit = document.getElementById("city");
			cityEdit.value = '';

			var stateEdit = document.getElementById("state");
			stateEdit.value = '';

			document.getElementById("search_results").innerHTML = '';
	}

	function selectItem(id, personName) {
		log('Selected Item: ' + id);
		log('Select Code: <%=selectParam%>');
		padre.receivePersonPick(id, personName, '<%=selectParam%>');
		window.close();	
	}

 	function handleXmlSearchResponse() {
			  // if the readyState code is 4 (Completed)
			  // and http status is 200 (OK) we go ahead and get the responseText
			  // other readyState codes:
			  // 0=Uninitialised 1=Loading 2=Loaded 3=Interactive
  			  log("Response received from server, processing XML response.");
			  var xmlhttp = getXmlHttp();
			  if (xmlhttp.readyState == 4) {
					if (xmlhttp.status == 200) {
							xmlDoc =
								AjxXmlDoc.createFromXml(xmlhttp.responseText);

							nativeDocument = xmlDoc.getDoc();
							currentPersonNodes = nativeDocument.getElementsByTagName("person");
							displayPersonNodes(currentPersonNodes);
					} else {
	  			  		log("Not processed - Response status = " + xmlhttp.status);
					}
			  } else {
  			  		log("Not processed - Response readyState = " + xmlhttp.readyState);
			  }
	}


	function displayPersonNodes(currentPersonNodes) {
		var resultsTable = '<table width="550" style="cursor: pointer">';
		resultsTable += '<tr>'
			+ '<td><font class="subFont"><b>Id</b></font></td>'
			+ '<td><font class="subFont"><b>Last Name</b></font></td>'
			+ '<td><font class="subFont"><b>First Name</b></font></td>'
			+ '<td><font class="subFont"><b>Home Phone</b></font></td>'
			+ '<td><font class="subFont"><b>City</b></font></td>'
			+ '<td><font class="subFont"><b>State</b></font></td>'
			+ '</tr>';
		var normalRowColor = '#eeee88';
		var selectRowColor = '#cccccc';
		var quotedNormalRowColor = "'" + normalRowColor + "'";
		var quotedSelectRowColor = "'" + selectRowColor + "'";
		for (i = 0; i < currentPersonNodes.length; i++) {
			node = currentPersonNodes[i];
			var quotedId = "'" + node.getAttribute("id") + "'";
			var quotedName = "'" + node.getAttribute("lastName") + " " + node.getAttribute("firstName") + "'";
			log('quotedId=' + quotedId);
			var trStr;
			trStr = '<tr'; 
			trStr += ' onClick="selectItem(' + quotedId + ', ' + quotedName + ');"';
			trStr += ' onMouseOver="changeColor(this, '	+ quotedSelectRowColor + ');"';
			trStr += ' onMouseOut="changeColor(this, '	+ quotedNormalRowColor + ');"';
			trStr += '>';
			log(trStr);
			
			var detailStr = trStr
				+ '<td><font class="subFont">' + node.getAttribute("id") + '</font></td>'
				+ '<td><font class="subFont">' + node.getAttribute("lastName") + '</font></td>'
				+ '<td><font class="subFont">' + node.getAttribute("firstName") + '</font></td>'
				+ '<td><font class="subFont">' + node.getAttribute("homePhone") + '</font></td>'
				+ '<td><font class="subFont">' + node.getAttribute("city") + '</font></td>'
				+ '<td><font class="subFont">' + node.getAttribute("state") + '</font></td>'
				+ '</tr>';
			resultsTable += detailStr;
		}
		resultsTable += '</table>';
		log('Setting table to new contents');
	    document.getElementById("search_results").innerHTML = resultsTable;
		log('Done - Setting table to new contents');
	}

	//Method to receive person pick dialog selection
	function receivePersonPick(personId, lastName, firstName, middleName) {
	    var paramName = personPickParamName;
	    var inputId = 'person_picker_' + paramName;
	    log('Finding DIV that displays person name - element id = ' + inputId);
	    var personDiv = document.getElementById(inputId);
	    personDiv.innerHTML = '<div class="stdFont">' + firstName + '&nbsp;' + lastName +  '</div>';
	    var hidden = document.getElementById(paramName);
	    hidden.value = personId;
	};

 -->
</script>


<table width="100%">
	<tr>
		<td>
			<vectrics:personSearchForm personId="<%=personId%>"/>
		</td>
	</tr>
	<tr>
		<td>
			<div id="search_results" style="top:10px; left:10px; width:90%; height:300px;
				background-color:#dddda0; text-align:left; padding:0px;border: solid 1px #339999;
				z-index:0;overflow:auto;">
			</div>
		</td>
	</tr>
</table>

</div>


